package edu.iit.cs553;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.joda.time.DateTime;

import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient;
import com.amazonaws.services.cloudwatch.model.Datapoint;
import com.amazonaws.services.cloudwatch.model.Dimension;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult;
import com.amazonaws.services.cloudwatch.model.StandardUnit;
import com.amazonaws.services.sqs.AmazonSQSClient;

public class CloudWatchMonitor {

	private AmazonCloudWatchClient cloudWatchClient;
	private AmazonSQSClient sqsClient;

	public CloudWatchMonitor()
	{
		this.cloudWatchClient = new AmazonCloudWatchClient(new ClasspathPropertiesFileCredentialsProvider());
		this.sqsClient = new AmazonSQSClient(new ClasspathPropertiesFileCredentialsProvider());
		cloudWatchClient.setEndpoint("monitoring.us-west-2.amazonaws.com");
		sqsClient.setEndpoint("sqs.us-west-2.amazonaws.com");
	}

	public int getNumberOfTasksInQueue()
	{
		double numberOfTasksSentToQueue = getNumberOfTasksSentToQueue();
		double numberOfTasksDeletedFromQueue = getNumberOfTasksDeletedFromQueue();
		double numberOfTasksInQueue = numberOfTasksSentToQueue - numberOfTasksDeletedFromQueue;
		return (int) numberOfTasksInQueue;
	}

	private double getNumberOfTasksSentToQueue()
	{
		GetMetricStatisticsRequest request = new GetMetricStatisticsRequest();

		request.setNamespace("AWS/SQS");
		request.setMetricName("NumberOfMessagesSent");

		Collection<Dimension> dimensions = new ArrayList<Dimension>();
		Dimension dimension = new Dimension();
		dimension.setName("QueueName");
		dimension.setValue("TasksQueue");
		dimensions.add(dimension);

		request.setDimensions(dimensions);
		request.setUnit(StandardUnit.Count);
		request.setPeriod(new Integer(200 * 3600));

		// DateTime startTimeTemp = new DateTime();
		// long startTimeTempInLong = startTimeTemp.getMillis();
		// DateTime startTime = new DateTime(startTimeTempInLong - 2 * 60 *
		// 1000);
		// long startTimeInLong = startTime.getMillis();
		// DateTime endTime = new DateTime(startTimeInLong + 24 * 3600 * 1000);
		//
		// request.setStartTime(new Date(startTime.getMillis()));
		// request.setEndTime(new Date(endTime.getMillis()));

		DateTime startTime = new DateTime(2013, 5, 4, 0, 0);
		DateTime endTime = new DateTime(2013, 5, 6, 0, 0);

		request.setStartTime(new Date(startTime.getMillis()));
		request.setEndTime(new Date(endTime.getMillis()));

		System.out.println();

		Collection<String> statistics = new ArrayList<String>();
		statistics.add("Sum");
		request.setStatistics(statistics);

		GetMetricStatisticsResult result = cloudWatchClient.getMetricStatistics(request);
		List<Datapoint> datapoints = result.getDatapoints();
		for (Datapoint datapoint : datapoints)
		{
			System.out.println("Number of tasks sent: " + datapoint.getSum());
		}
		if (!datapoints.isEmpty())
		{
			return datapoints.get(0).getSum();
		} else
		{
			System.out.println("Number of tasks sent: " + 0);
			return 0;
		}
	}

	private double getNumberOfTasksDeletedFromQueue()
	{
		GetMetricStatisticsRequest request = new GetMetricStatisticsRequest();

		request.setNamespace("AWS/SQS");
		request.setMetricName("NumberOfMessagesDeleted");

		Collection<Dimension> dimensions = new ArrayList<Dimension>();
		Dimension dimension = new Dimension();
		dimension.setName("QueueName");
		dimension.setValue("TasksQueue");
		dimensions.add(dimension);

		request.setDimensions(dimensions);
		request.setUnit(StandardUnit.Count);
		request.setPeriod(new Integer(200 * 3600));

		DateTime startTime = new DateTime(2013, 5, 4, 0, 0);
		DateTime endTime = new DateTime(2013, 5, 6, 0, 0);

		request.setStartTime(new Date(startTime.getMillis()));
		request.setEndTime(new Date(endTime.getMillis()));

		// DateTime startTimeTemp = new DateTime();
		// long startTimeTempInLong = startTimeTemp.getMillis();
		// DateTime startTime = new DateTime(startTimeTempInLong - 2 * 60 *
		// 1000);
		// long startTimeInLong = startTime.getMillis();
		// DateTime endTime = new DateTime(startTimeInLong + 24 * 3600 * 1000);
		//
		// request.setStartTime(new Date(startTime.getMillis()));
		// request.setEndTime(new Date(endTime.getMillis()));

		System.out.println();

		Collection<String> statistics = new ArrayList<String>();
		statistics.add("Sum");
		request.setStatistics(statistics);

		GetMetricStatisticsResult result = cloudWatchClient.getMetricStatistics(request);
		List<Datapoint> datapoints = result.getDatapoints();
		for (Datapoint datapoint : datapoints)
		{
			// System.out.println("Datapoint n #" + i + " " +
			// datapoint.getSum());
			System.out.println("Number of tasks deleted: " + datapoint.getSum());
		}
		if (!datapoints.isEmpty())
		{
			return datapoints.get(0).getSum();
		} else
		{
			System.out.println("Number of tasks deleted: " + 0);
			return 0;
		}
	}
}
